🎖️GitЯра🎖️
Node / meshtastic / Meshtastic-Android / files / app / src / main / java / com / geeksville / mesh / concurrent / SyncContinuation.kt
Displaying Raw • Download
app/src/main/java/com/geeksville/mesh/concurrent/SyncContinuation.kt 78274c792318331b11ab64cd28e58e829875b8c2 (78274c79) Text, 3.08 KB
T8b949e/*
* Copyright (c) 2025 Meshtastic LLC
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
Tff7b72package T7ee787com.geeksville.mesh.concurrent
T8b949e/** A deferred execution object (with various possible implementations) */
Tff7b72interface T56d364ContinuationTff7b72<Tff7b72in Te6edf3TTff7b72> Tb4b4b4{
Tff7b72abstract Tff7b72fun Td2a8ffresumeTb4b4b4(Te6edf3resTb4b4b4: Te6edf3ResultTff7b72<Te6edf3TTff7b72>Tb4b4b4)
T8b949e// syntactic sugar
Tff7b72fun Td2a8ffresumeSuccessTb4b4b4(Te6edf3resTb4b4b4: Te6edf3TTb4b4b4) Tff7b72= Te6edf3resumeTb4b4b4(Te6edf3ResultTb4b4b4.Te6edf3successTb4b4b4(Te6edf3resTb4b4b4)Tb4b4b4)
Tff7b72fun Td2a8ffresumeWithExceptionTb4b4b4(Te6edf3exTb4b4b4: Te6edf3ThrowableTb4b4b4) Tff7b72= Tff7b72try Tb4b4b4{
Te6edf3resumeTb4b4b4(Te6edf3ResultTb4b4b4.Te6edf3failureTb4b4b4(Te6edf3exTb4b4b4)Tb4b4b4)
Tb4b4b4} Tff7b72catch Tb4b4b4(Te6edf3exTb4b4b4: Te6edf3ThrowableTb4b4b4) Tb4b4b4{
T8b949e// Timber.e("Ignoring $ex while resuming, because we are the ones who threw it")
Tff7b72throw Te6edf3ex
Tb4b4b4}
Tb4b4b4}
T8b949e/** An async continuation that just calls a callback when the result is available */
Tff7b72class T56d364CallbackContinuationTff7b72<Tff7b72in Te6edf3TTff7b72>Tb4b4b4(Tff7b72private Tff7b72val Te6edf3cbTb4b4b4: Tb4b4b4(Te6edf3ResultTff7b72<Te6edf3TTff7b72>Tb4b4b4) Tff7b72-Tff7b72> Tffa657UnitTb4b4b4) Tb4b4b4: Te6edf3ContinuationTff7b72<Te6edf3TTff7b72> Tb4b4b4{
Tff7b72override Tff7b72fun Td2a8ffresumeTb4b4b4(Te6edf3resTb4b4b4: Te6edf3ResultTff7b72<Te6edf3TTff7b72>Tb4b4b4) Tff7b72= Te6edf3cbTb4b4b4(Te6edf3resTb4b4b4)
Tb4b4b4}
T8b949e/**
* This is a blocking/threaded version of coroutine Continuation
*
* A little bit ugly, but the coroutine version has a nasty internal bug that showed up in my SyncBluetoothDevice so I
* needed a quick workaround.
*/
Tff7b72class T56d364SyncContinuationTff7b72<Te6edf3TTff7b72> Tb4b4b4: Te6edf3ContinuationTff7b72<Te6edf3TTff7b72> Tb4b4b4{
Tff7b72private Tff7b72val Te6edf3mbox Tff7b72= Te6edf3javaTb4b4b4.Te6edf3langTb4b4b4.Te6edf3ObjectTb4b4b4(Tb4b4b4)
Tff7b72private Tff7b72var Te6edf3resultTb4b4b4: Te6edf3ResultTff7b72<Te6edf3TTff7b72>Tff7b72? Tff7b72= Tff7b72null
Tff7b72override Tff7b72fun Td2a8ffresumeTb4b4b4(Te6edf3resTb4b4b4: Te6edf3ResultTff7b72<Te6edf3TTff7b72>Tb4b4b4) Tb4b4b4{
Te6edf3synchronizedTb4b4b4(Te6edf3mboxTb4b4b4) Tb4b4b4{
Te6edf3result Tff7b72= Te6edf3res
Te6edf3mboxTb4b4b4.Te6edf3notifyTb4b4b4(Tb4b4b4)
Tb4b4b4}
Tb4b4b4}
T8b949e// Wait for the result (or throw an exception)
Tff7b72fun Td2a8ffawaitTb4b4b4(Te6edf3timeoutMsecsTb4b4b4: Tffa657Long Tff7b72= T79c0ff0Tb4b4b4)Tb4b4b4: Te6edf3T Tb4b4b4{
Te6edf3synchronizedTb4b4b4(Te6edf3mboxTb4b4b4) Tb4b4b4{
Tff7b72val Te6edf3startT Tff7b72= Te6edf3SystemTb4b4b4.Te6edf3currentTimeMillisTb4b4b4(Tb4b4b4)
Tff7b72while Tb4b4b4(Te6edf3result Tff7b72=Tff7b72= Tff7b72nullTb4b4b4) Tb4b4b4{
Te6edf3mboxTb4b4b4.Te6edf3waitTb4b4b4(Te6edf3timeoutMsecsTb4b4b4)
Tff7b72if Tb4b4b4(Te6edf3timeoutMsecs Tff7b72> T79c0ff0 Tff7b72&Tff7b72& Tb4b4b4(Tb4b4b4(Te6edf3SystemTb4b4b4.Te6edf3currentTimeMillisTb4b4b4(Tb4b4b4) Tff7b72- Te6edf3startTTb4b4b4) Tff7b72>Tff7b72= Te6edf3timeoutMsecsTb4b4b4)Tb4b4b4) Tb4b4b4{
Tff7b72throw Te6edf3ExceptionTb4b4b4(Ta5d6ff"Ta5d6ffSyncContinuation timeoutTa5d6ff"Tb4b4b4)
Tb4b4b4}
Tb4b4b4}
Tff7b72val Te6edf3r Tff7b72= Te6edf3result
Tff7b72if Tb4b4b4(Te6edf3r Tff7b72!Tff7b72= Tff7b72nullTb4b4b4) Tb4b4b4{
Tff7b72return Te6edf3rTb4b4b4.Te6edf3getOrThrowTb4b4b4(Tb4b4b4)
Tb4b4b4} Tff7b72else Tb4b4b4{
Tff7b72throw Te6edf3ExceptionTb4b4b4(Ta5d6ff"Ta5d6ffThis shouldn't happenTa5d6ff"Tb4b4b4)
Tb4b4b4}
Tb4b4b4}
Tb4b4b4}
Tb4b4b4}
T8b949e/**
* Calls an init function which is responsible for saving our continuation so that some other thread can call resume or
* resume with exception.
*
* Essentially this is a blocking version of the (buggy) coroutine suspendCoroutine
*/
Tff7b72fun Tff7b72<Te6edf3TTff7b72> Td2a8ffsuspendTb4b4b4(Te6edf3timeoutMsecsTb4b4b4: Tffa657Long Tff7b72= Tff7b72-T79c0ff1Tb4b4b4, Te6edf3initfnTb4b4b4: Tb4b4b4(Te6edf3SyncContinuationTff7b72<Te6edf3TTff7b72>Tb4b4b4) Tff7b72-Tff7b72> Tffa657UnitTb4b4b4)Tb4b4b4: Te6edf3T Tb4b4b4{
Tff7b72val Te6edf3cont Tff7b72= Te6edf3SyncContinuationTff7b72<Te6edf3TTff7b72>Tb4b4b4(Tb4b4b4)
T8b949e// First call the init funct
Te6edf3initfnTb4b4b4(Te6edf3contTb4b4b4)
T8b949e// Now wait for the continuation to finish
Tff7b72return Te6edf3contTb4b4b4.Te6edf3awaitTb4b4b4(Te6edf3timeoutMsecsTb4b4b4)
Tb4b4b4}
Served by rngit 1.5.0 - Generated in 0.06s